/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/////////////////////////////////////////////////////////////////////////

package sheep.view;

/////////////////////////////////////////////////////////////////////////

import sheep.model.graph.Graph;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.AbstractLayout;
import edu.uci.ics.jung.visualization.DefaultSettableVertexLocationFunction;
import edu.uci.ics.jung.visualization.FRLayout;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.ShapePickSupport;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Dimension;
import java.awt.Graphics;

import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.StackLayout;

import sheep.JUNG.utils.OurGrapheMouseListener;
import sheep.JUNG.utils.OurModalGraphMouse;
import sheep.JUNG.utils.OurPluggableRenderer;
import sheep.JUNG.utils.OurPopupGraphMousePlugin;
import sheep.JUNG.utils.OurVertexShape;
import sheep.controller.Workspace;
import sheep.model.Task.GraphMode;
import sheep.view.Component.SheepScrollbarUI;
import sheep.view.jung.utils.*;
/////////////////////////////////////////////////////////////////////////

/**
 *
 * @author geek
 */
public class GrapheViewer extends JXPanel{


  public void setRecenterOnClickEnabled(boolean b) {
  
  }

  public void setZoom(int i) {
   
  }

  public int getZoom() {
    return 1;
  }
    
  public void setZoomScale(float f) {
   
  }
  
  public float getZoomScale() {
   return 1.0f;
  }
  
  
  
  
  public void paintComponent(Graphics g){
    super.paintComponent(g);
  }
  
  private boolean init = false;
  
   protected  Vertex root; 
   protected  Vertex[] v; 
   protected  Edge []e; 
  
   protected  Graph g;
   protected  VisualizationViewer vv; 
   //protected PluggableRenderer pr; 
  
   AbstractLayout layout;


    
   private Workspace workSpace;
   
   
   	// Gestionnaire d'affichages, formes, couleurs,...
	protected OurPluggableRenderer pr; 
	// Gestionnaire de souris
	protected OurModalGraphMouse gm;
        
   /////////////////////////////////////////////////////////////////////////
   
   public GrapheViewer(Workspace workSpace){
  
     this.workSpace = workSpace;
     
     if (this.workSpace.getCurrentTask()!=null)
        g= this.workSpace.getCurrentTask().getGraph();
     else{
       g  = new Graph(workSpace);
       gm = new OurModalGraphMouse(workSpace);	    
       gm.add(new OurPopupGraphMousePlugin(workSpace));	    
       gm.setMode(ModalGraphMouse.Mode.PICKING);
     }

     
     if (this.workSpace.getCurrentTask()==null){
        pr = new OurPluggableRenderer();
        layout = new FRLayout(g);
        pr.setVertexShapeFunction(new OurVertexShape(layout,workSpace));	    
        pr.setVertexPaintFunction(new OurVertexShape(layout,workSpace));	    
        pr.setVertexStringer(new VertexLabel());            
        pr.setEdgeShapeFunction(new EdgeShape.Line());
        pr.setEdgePaintFunction(new OurVertexShape(layout,workSpace));
        DefaultSettableVertexLocationFunction vertexLocations = new DefaultSettableVertexLocationFunction(); 
        
        layout.initialize(new Dimension(800,600));
        gm.setVertexLocations(vertexLocations);
        vv = new VisualizationViewer(layout,pr);
        vv.setPickSupport(new ShapePickSupport());
        vv.setFocusable(true);
        
     }else{

         layout= g.getAutoLayout();
         gm.setVertexLocations(g.getAutoVertexLocations());
         vv = g.getAutoVv();         
     }
        
     vv.getModel().restart();
     
     vv.setGraphMouse(gm);
     
     
     vv.addGraphMouseListener(new OurGrapheMouseListener(this.workSpace));
     
     this.setLayout(new StackLayout());
     GraphZoomScrollPane graphZoomScrollPane = new GraphZoomScrollPane(vv);
     graphZoomScrollPane.getVerticalScrollBar().setUI(new SheepScrollbarUI());
     graphZoomScrollPane.getHorizontalScrollBar().setUI(new SheepScrollbarUI());
     this.add(graphZoomScrollPane,BorderLayout.CENTER);
     
     vv.repaint();
     graphZoomScrollPane.repaint();
     repaint();
     paintAll(getGraphics());
     refreshGraph();

   }


  @Override
  public void paintComponents(Graphics g) {
    if(!init){
      refreshGraph();
      init = true;
    }
    super.paintComponents(g);
  }
  
  
   public void refreshGraph(){
     if (this.workSpace.getCurrentTask()!=null){
        g= this.workSpace.getCurrentTask().getGraph();
     }else
        g  = new Graph(workSpace);
 
     if (this.workSpace.getCurrentTask()!=null){
       
       if (this.workSpace.getCurrentTask().getGraphMode()== GraphMode.NORMAL){
         layout= g.getLayout();
         gm.setVertexLocations(g.getVertexLocations());
         vv = g.getVv();
       }else{
         layout= g.getAutoLayout();
         gm.setVertexLocations(g.getAutoVertexLocations());
         vv = g.getAutoVv();         
       }
         
       vv.getModel().restart();
     }
     //System.out.println("graphe viewer :" + workSpace.getCurrentTask().getName());
     //System.out.println("graphe viewer graphe :" + workSpace.getCurrentTask().getGraph().getName());
     
     vv.setGraphMouse(gm);

     vv.addGraphMouseListener(new OurGrapheMouseListener(this.workSpace));
     
     this.setLayout(new StackLayout());
     GraphZoomScrollPane graphZoomScrollPane = new GraphZoomScrollPane(vv);
     graphZoomScrollPane.getVerticalScrollBar().setUI(new SheepScrollbarUI());
     graphZoomScrollPane.getHorizontalScrollBar().setUI(new SheepScrollbarUI());
     this.add(graphZoomScrollPane,BorderLayout.CENTER);
     
   
     vv.repaint();
     graphZoomScrollPane.repaint();
     repaint();
     paintAll(getGraphics());
   }   

///////////////////////////////////////////////////////////////////////// 
     
}

/////////////////////////////////////////////////////////////////////////
